//
//  LFWelComeViewController.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/23.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit
import SDWebImage

class LFWelComeViewController: UIViewController {
    
    //头像底部约束 应该保存起来
    var iconViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //取出沙盒里面的缓存
        if  let imageString = LFGlobalUserAccountsViewModel.sharedUserAccounts.userAccounts?.avatar_large {
            
            let imageURL = NSURL(string: imageString)
            
            self.iconView.sd_setImageWithURL(imageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        }
      
        //加载欢迎头像
        LFGlobalUserAccountsViewModel.sharedUserAccounts.loadUserAccountsMessage { (error) -> () in
            if error != nil {
                print("头像加载出错")
                return
            }
            
            let imageString = LFGlobalUserAccountsViewModel.sharedUserAccounts.userAccounts?.avatar_large
            
                let imageURL = NSURL(string: imageString!)
            
                self.iconView.sd_setImageWithURL(imageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //UI布局
        prepareUI()
        
    }
    
    
    //MARK : -- UI布局
    private func prepareUI() {
        
        //1.添加
        self.view.addSubview(bagImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(tipLabel)
        
        // 2.添加约束
        bagImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        tipLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        self.view.addConstraint(NSLayoutConstraint(item: bagImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bagImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bagImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bagImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 头像
        // centerX
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
         self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 底部约束
        // 记录底部约束
        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewBottomCons!)
        
        // 欢迎label
        self.view.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
    }
    
    //在界面显示完成之后 完成头像动画，并跳转主界面
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        UIView .animateWithDuration(0.5, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //更改头像位置
            self.iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            //刷新界面
            self.view.layoutIfNeeded()
            
            }, completion: { (_) -> Void in
                self.tipLabel.hidden = false
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                    //跳转主界面
                   AppDelegate.JumpViewController(LFMainViewController())
                })
        })
        
    }
    
    
    
    //MARK : -- 懒加载
    private lazy var bagImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        let icon = UIImageView()
        
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        
        return icon
    }()
    
    private lazy var tipLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(16)
        label.sizeToFit()
        label.hidden = true
        
        return label
    }()
    
}
